package com.martin.videoplayer.video;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cxf on 2018/11/28.
 */

public class VideoPreviewAdapter extends RecyclerView.Adapter<VideoPreviewAdapter.Vh> {

    private LayoutInflater mInflater;
    private ActionListener mActionListener;
    private LinearLayoutManager mLayoutManager;
    private int mCurPosition;
    private Context mContext;
    private List<String> mDataList = new ArrayList<>();

    public VideoPreviewAdapter(Context context, List<String> dataList) {
        mDataList = dataList;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public Vh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_preview_video, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Vh vh, int position) {
        vh.setData(position);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    class Vh extends RecyclerView.ViewHolder {

        VideoPlayerStandard mVideoView;

        public Vh(View itemView) {
            super(itemView);
            mVideoView = itemView.findViewById(R.id.video_player);
        }

        void setData(int position) {
            if (mActionListener != null) {
                mActionListener.loadVideo(mVideoView, position);
            }
        }
    }

    public void setActionListener(ActionListener actionListener) {
        mActionListener = actionListener;
    }

    public void setCurPosition(int curPosition) {
        mCurPosition = curPosition;
        if (mActionListener != null) {
            mActionListener.onPageChanged(curPosition);
        }
    }

    public int getCurPosition() {
        return mCurPosition;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(recyclerView);
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                int position = mLayoutManager.findFirstCompletelyVisibleItemPosition();
                if (position >= 0 && mCurPosition != position) {
                    mCurPosition = position;
                    if (mActionListener != null) {
                        mActionListener.onPageChanged(position);
                    }
                }
            }
        });
    }


    public interface ActionListener {
        void onPageChanged(int position);

        void loadVideo(VideoPlayerStandard videoView, int position);
    }
}
